<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 13:23
 */

namespace app\admin\controller;

//客户资源管理
class Customer extends Common
{
    /*
     * 客户资源统计-列表
     */
    public function index()
    {
        $data = [];
        //搜索
        $telephone = input('post.telephone');
        $data['telephone'] = $telephone;
        $company_name = input('post.company_name');
        $data['company_name'] = $company_name;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new \app\admin\model\Company();
        //列表数据
        $list = $model->getList($pageNum, $numPerPage, $data['company_name'],$data['telephone']);
        $data['list'] = $list;
        //数据总数
        $count = $model->getCount($data['company_name'],$data['telephone']);
        $data['count'] = $count;
        return view('', $data);
    }
    /**
     * @return \think\response\View
     */
    public function csdlist()
    {
        $param      = input('post.');
        $company_id = input('param.cid');
        $data       = [];
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage          = config("num_per_page");
        $data['numPerPage']  = $numPerPage;
        if(!empty($param['mobile'])) {
    		$data['mobile'] = ['like',"%{$param['mobile']}%"];
    	}
    	$data['mobile'] = $param['mobile'];

    	if(!empty($param['user_name'])) {
    		$data['user_name'] = ['like',"%{$param['user_name']}%"];
    	}
    	$data['user_name'] = $param['user_name'];

    	$data['license_type'] = $param['license_type'];
        $order_model = new \app\admin\model\CsdList();
        //列表数据
        $list         = $order_model->getorderist($pageNum, $numPerPage, $param, $company_id);
        $data['list'] = $list;
        $data['cid']  = $company_id;
        //数据总数
        $count                  = $order_model->getorderount($param, $company_id);

        $data['license_number'] = $param['license_number'];
        if (!empty($param['start_time'])) {
            $data['start_time'] = date('Y-m-d', $param['start_time']);
        }
        if (!empty($param['end_time'])) {
            $data['end_time'] = date('Y-m-d', $param['end_time']);
        }
        $data['count'] = $count;
        $data['vehicle'] = db('vehicle')->select();
        return view('', $data);
    }

    /**
     * @return \think\response\View
     * 订单列表-查看
     */
    public function orderList() {

        $data = input('');
        $vehicle_identification = $data['vehicle_identification'];
        $order_ids = array();
        $driving_infos = db('driving_license')
                        ->where('vehicle_identification',$vehicle_identification)
                        ->field('order_id')
                        ->select();
        foreach ($driving_infos as $key => $value) {
            array_push($order_ids, $value['order_id']);
        }
        $map['o.order_id'] = array('in',array_unique($order_ids));
        if ($data['is_distribut'] != '') {
            $map['o.is_distribut'] = $data['is_distribut'];
        }
        if ($data['order_sn'] != '') {
            $map['o.order_sn'] = $data['order_sn'];
        }

        $data['order_status'] = -1;
        if (is_numeric(input('post.order_status'))) {
            $data['order_status'] = input('post.order_status');
        }

        if($data['order_status'] != -1 ) {
            $map['o.order_status'] = $data['order_status'];
        }
//        else{
//            $map['o.order_status'] = ['in','2,3'];
//        }
        if($data['order_sn'] != ''){
            $map['o.order_sn'] = ['like',"%{$data['order_sn']}%"];
        }


        if(!empty($data['start_time']) && !empty($data['end_time'])) {
            $map['o.create_time'] = ['between time',[$data['start_time'],$data['end_time']]];
        } elseif (!empty($data['start_time']) && empty($data['end_time'])) {
            $map['o.create_time'] = ['between time',[$data['start_time'],date("Y-m-d H:i:s")]];
        } elseif (empty($data['start_time']) && !empty($data['end_time'])) {
            $map['o.create_time'] = ['between time',["1970-01-01 00:00:00",$data['end_time']]];
        }

        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;

        $map['o.company_id'] = $data['cid'];
        $list = db('order')
            ->alias('o')
            ->join('driving_license d', 'd.order_id = o.order_id')
            ->where($map)
            ->order('d.update_time desc')
            ->page($pageNum, $numPerPage)
            ->select();
        $count = db('order')
            ->alias('o')
            ->join('driving_license d', 'd.order_id = o.order_id','LEFT')
            ->where($map)
            ->count();

        $data['list'] = $list;
        $data['count'] = $count;
        return view('',$data);
    }
    /*
     * 客户资源统计-查看
     */
    public function custone()
    {
        $data               = [];
        $map['id']          = input('get.id');
        // $map['is_delete']   = 0;
        $dl = db("driving_license")
                ->field("open_id")
                ->where($map)
                ->find();
        $user = db("users")
                ->field("user_id,open_id,create_time,update_time,company_id,birthday,address,mobile")
                ->where('open_id',$dl['open_id'])
                // ->where('is_delete',0)
                ->find();
        $userwx = db("user_weixin")
                    ->where("open_id",$dl['open_id'])
                    ->find();
        $user['appid'] = $userwx['appid'];
        $user['nick_name'] = $userwx['nick_name'];
        $user['gender'] = $userwx['gender'];
        $user['province'] = $userwx['province'];
        $user['city'] = $userwx['city'];
        $user['country'] = $userwx['country'];

        unset($user['company_id']);
        unset($user['user_id']);
        $data['users'] = $user;
        return view('', $data);
    }
}
